package com.cya.lotterysystem.controller;

import com.cya.lotterysystem.common.errcode.ControllerErrorCodeConstans;
import com.cya.lotterysystem.common.exception.ControllerException;
import com.cya.lotterysystem.common.pojo.CommonResult;
import com.cya.lotterysystem.common.utils.JacksonUtil;
import com.cya.lotterysystem.controller.param.CreatePrizeParam;
import com.cya.lotterysystem.controller.param.PageParam;
import com.cya.lotterysystem.controller.reslut.FindPrizeListResult;
import com.cya.lotterysystem.service.PictureService;
import com.cya.lotterysystem.service.PrizeService;
import com.cya.lotterysystem.service.dto.PageListDTO;
import com.cya.lotterysystem.service.dto.PrizeDTO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/11/1 21:01
 * @description：
 * @modified By：
 * @version:
 */
@Slf4j
@RestController
public class PrizeController {

    @Autowired
    private PrizeService prizeService;

    @Autowired
    private PictureService pictureService;

    @RequestMapping("/pic/uplod")
    public String uploadPic(MultipartFile file){
        return pictureService.savePicture(file);
    }

    /** @RequestPart 用于接受表单格式 multipart.from-data
     * 创建奖品
     * @param param
     * @param picfile
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Valid @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picfile){


    log.info("prizePic CreatePrizeParam:{}", JacksonUtil.writeValueAsString(param));

    return CommonResult.success(
            prizeService.createPrize(param,picfile)
    );





    }

    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param){

        log.info("findPrizeList PageParam:{} ",JacksonUtil.writeValueAsString(param));

        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(converFindPrizeListResult(pageListDTO));
    }

    private FindPrizeListResult converFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {

        if (pageListDTO==null){
            throw new ControllerException(ControllerErrorCodeConstans.FIND_PRIZE_ERROR);

        }
        FindPrizeListResult result=new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
      result.setRecords(pageListDTO.getRecords().stream()
              .map(prizeDTO -> {
                  FindPrizeListResult.PrizeInfo prizeInfo=new FindPrizeListResult.PrizeInfo();

                  prizeInfo.setPrizeId(prizeDTO.getPrizeId());

                  prizeInfo.setPrizeName(prizeDTO.getName());

                  prizeInfo.setDescription(prizeDTO.getDescription());

                  prizeInfo.setPrice(prizeDTO.getPrice());

                  prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                  return prizeInfo;
              }).collect(Collectors.toList()));

        return result;
    }
}